<template>
    <el-dialog :title="!dataForm.pkType ? '新增' : '修改'" destroy-on-close :before-close="closeDailog"
        :close-on-click-modal="false" :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="80px">
            <el-form-item label="参数类型" prop="dictType">
                <el-input clearable v-model="dataForm.dictType" placeholder="参数类型"></el-input>
            </el-form-item>
            <el-form-item label="参数描述" prop="dictDesc">
                <el-input clearable v-model="dataForm.dictDesc" placeholder="参数描述"></el-input>
            </el-form-item>
            <el-form-item label="字典类型" prop="dictMode">
                <el-select v-model="dataForm.dictMode" clearable placeholder="请选择类型" style="width:100%">
                    <el-option v-for="item in dictModeOptions" :key="item.value" :label="item.label"
                        :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="字典备注" prop="dictNote">
                <el-input clearable v-model="dataForm.dictNote" placeholder="字典备注"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="closeDailog">取消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    //   import API from '@/api'
    import * as dictService from '@/api/bdms/dict'
    export default {
        data() {
            return {
                visible: false,
                submitLoading: false,
                dataForm: {
                    pkType: 0,
                    dictType: '',
                    dictDesc: '',
                    dictMode: '',
                    dictNote: '',
                    revision: 0
                },
                dictModeOptions: [],
                dataRule: {
                    dictType: [{
                        required: true,
                        message: '参数类型不能为空',
                        trigger: 'blur'
                    }],
                    dictDesc: [{
                        required: true,
                        message: '参数描述不能为空',
                        trigger: 'blur'
                    }],
                    dictMode: [{
                        required: true,
                        message: '字典类型不能为空',
                        trigger: 'blur'
                    }],
                    dictNote: [{
                        required: true,
                        message: '字典备注不能为空',
                        trigger: 'blur'
                    }]
                }
            }
        },
        watch:{
            dataForm:{
                handler: function(){
                    this.dataForm.dictNote = this.dataForm.dictDesc + this.dataForm.dictType
                },
                deep: true
            }
        },
        methods: {
            init(id) {
                this.getDict();
                this.dataForm.pkType = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.pkType) {
                        dictService.getDictType(this.dataForm.pkType).then(data => {
                            this.dataForm.dictType = data.dictType
                            this.dataForm.dictDesc = data.dictDesc
                            this.dataForm.dictMode = data.dictMode
                            this.dataForm.dictNote = data.dictNote
                            this.dataForm.revision = data.revision
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.submitLoading = true
                        var params = {
                            'pkType': this.dataForm.pkType || undefined,
                            'dictType': this.dataForm.dictType,
                            'dictDesc': this.dataForm.dictDesc,
                            'dictMode': this.dataForm.dictMode,
                            'dictNote': this.dataForm.dictNote,
                            'revision': this.dataForm.revision
                        }
                        var tick = !this.dataForm.pkType ? dictService.saveDictType(params) : dictService
                            .updateDictType(
                                params)
                        tick.then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.submitLoading = false
                                    this.closeDailog()
                                    this.$emit('refreshDataList')
                                }
                            })
                        }).catch((res) => {
                            this.submitLoading = false
                            this.closeDailog()
                        })
                    }
                })
            },
            // 查询字典项
            getDict() {
                dictService.getValues("dict_mode").then(data => {
                    this.dictModeOptions = data;
                })
            },
            closeDailog() {
                this.$refs['dataForm'].resetFields()
                this.visible = false
            },
        }
    }
</script>